from datetime import time
from django.db import models
from .base_model import BaseModel
from .class_course import ClassCourse
class CoursePeriod(BaseModel):
    """课程时间段模型，作为ClassCourse的扩展字段"""
    name = models.CharField(max_length=50, verbose_name='时间段名称')
    start_time = models.TimeField(verbose_name='开始时间')
    duration = models.PositiveSmallIntegerField(default=45, verbose_name='课程时长(分钟)')
    
    @property
    def end_time(self):
        """自动计算结束时间"""
        if self.start_time:
            total_minutes = self.start_time.hour * 60 + self.start_time.minute
            total_minutes += self.duration
            hours, minutes = divmod(total_minutes, 60)
            hours %= 24
            return time(hour=hours, minute=minutes)
        return None
    
    def __str__(self):
        return f'{self.name} {self.start_time.strftime("%H:%M")}-{self.end_time.strftime("%H:%M") if self.end_time else "未知"}'
    
    class Meta:
        verbose_name = '课程时间段'
        verbose_name_plural = '课程时间段'